package com.application.lucky;

import android.app.ActivityManager;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.multidex.MultiDexApplication;

import com.application.lucky.gen.DaoMaster;
import com.application.lucky.gen.DaoSession;
import com.application.lucky.utils.CommonUtils;
import com.application.lucky.utils.TopActivityManager;
import com.application.mylibrary.utils.ToastUtils;
import com.baselib.libnetwork.net.HttpManager;
import com.baselib.libnetwork.net.libnetworkcomponent.RequestCommonParams;
import com.fm.openinstall.OpenInstall;
import com.qunar.im.ui.sdk.QIMSdk;
import com.tencent.bugly.crashreport.CrashReport;

public class Lucky extends MultiDexApplication {

    private static Lucky application;
    private SQLiteDatabase db;
    private DaoSession mDaoSession;
    public static boolean isLoginout = false;
    public static Lucky getApplication() {
        return application;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (BuildConfig.APPLICATION_ID.equals(CommonUtils.getCurProcessName(this))) {
//            initRouter();
            initDao();
            initIm();
            initNetWork();
            ToastUtils.init(this);
            initbugly();
            application = this;
            TopActivityManager instance = TopActivityManager.getInstance();
            registerActivityLifecycleCallbacks(instance);

        }

        if (isMainProcess()) {
            OpenInstall.init(this);
        }
    }


    private void initbugly() {
        CrashReport.initCrashReport(getApplicationContext(), "1b7e8c7ecc", false);
    }

    private void initNetWork() {
        HttpManager.getInstance().okHttp();
        RequestCommonParams.getInstance().onInit(this);

    }

    private void initIm() {
        QIMSdk.getInstance().init(this);
        // QIMSdk.getInstance().setNavigationUrl("http://47.56.119.120:8080/startalk_nav");
        QIMSdk.getInstance().setNavigationUrl("http://39.106.117.38:8080/startalk_nav");
        QIMSdk.getInstance().openDebug();
    }


    private void initDao() {
        DaoMaster.DevOpenHelper mHelper = new DaoMaster.DevOpenHelper(this, "sport-db", null);
        db = mHelper.getWritableDatabase();
        // 注意：该数据库连接属于 DaoMaster，所以多个 Session 指的是相同的数据库连接。
        DaoMaster mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    public boolean isMainProcess() {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return getApplicationInfo().packageName.equals(appProcess.processName);
            }
        }
        return false;
    }
}
